package com.daisuxia.risk.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpUtil {

	Logger logger = Logger.getLogger(getClass());
	public static HttpUtil httpUtil;

	public static HttpUtil getInstance() {
		if (httpUtil == null) {
			httpUtil = new HttpUtil();
		}
		return httpUtil;
	}

	public String doPost(String url, String params)
			throws ClientProtocolException, IOException {
		logger.info("请求参数:" + params.toString());
		String result = "";
		HttpPost httpPost = new HttpPost(url);
		StringEntity stringEntity = new StringEntity(params.toString(), "utf-8");
		httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");

		httpPost.setEntity(stringEntity);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse httpResponse = httpClient.execute(httpPost);
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					httpResponse.getEntity().getContent()));
			String resultStr = reader.readLine();
			logger.info("resultStr=" + resultStr);
			while (null != resultStr) {
				result = resultStr;
				resultStr = reader.readLine();
			}
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return result;
	}
}
